package org.example.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.example.domain.query.User;
import org.example.domain.vo.UserInfoVo;
import org.example.enums.ResponseEnum;
import org.example.model.SysUser;
import org.example.service.SysUserService;
import org.example.mapper.SysUserMapper;
import org.example.utils.IdWorker;
import org.example.utils.Response;
import org.example.domain.vo.CaptchaVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author camellia
 * @description 针对表【sys_user(用户表)】的数据库操作Service实现
 * @createDate 2024-06-14 10:32:14
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
        implements SysUserService {

    private final SysUserMapper sysUserMapper;
    private final PasswordEncoder passwordEncoder;
    private final RedisTemplate redisTemplate;

    public SysUserServiceImpl(SysUserMapper sysUserMapper, PasswordEncoder passwordEncoder, @Qualifier("redisTemplate") RedisTemplate redisTemplate) {
        this.sysUserMapper = sysUserMapper; 
        this.passwordEncoder = passwordEncoder;
        this.redisTemplate = redisTemplate;
    }

    @Override
    public Response<UserInfoVo> login(User user) {
        if (user == null || StringUtils.isBlank(user.getUsername()) || StringUtils.isBlank(user.getPassword()) || StringUtils.isBlank(user.getCode())) {
            return Response.error(ResponseEnum.DATA_ERROR);
        }
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUsername, user.getUsername());
        SysUser sysUser = sysUserMapper.selectOne(wrapper);
        if (sysUser == null) {
            return Response.error(ResponseEnum.ACCOUNT_NOT_EXISTS);
        }
        boolean matches = passwordEncoder.matches(user.getPassword(), sysUser.getPassword());
        if (!matches) {
            return Response.error(ResponseEnum.USERNAME_OR_PASSWORD_ERROR);
        }
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(sysUser, userInfoVo);

        return Response.success(userInfoVo);
    }

    @Override
    public SysUser getUserByUsername(String username) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUsername, username);
        return sysUserMapper.selectOne(wrapper);
    }

    @Override
    public Response<CaptchaVo> getCaptchaCode() {
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(250, 40, 4, 5);
        String code = lineCaptcha.getCode();
        String imageBase64 = lineCaptcha.getImageBase64();
        IdWorker idWorker = new IdWorker();
        String sessionId = String.valueOf(idWorker.nextId());
        ValueOperations<String,String> valueOperations = redisTemplate.opsForValue();
        valueOperations
                .set("CK:" + sessionId, code, 1, TimeUnit.MINUTES);
        CaptchaVo captchaRVo = new CaptchaVo();
        captchaRVo.setUrl(imageBase64);
        captchaRVo.setSessionId(sessionId);
        return Response.success(captchaRVo);
    }
}




